package com.orderplus.opshop.synces.service.product.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orderplus.opshop.common.util.MapUtil;
import com.orderplus.opshop.common.util.ValueUtil;
import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.synces.bean.ProductDetailModel;
import com.orderplus.opshop.synces.constant.EsConstant;
import com.orderplus.opshop.synces.entity.product.ProductProperty;
import com.orderplus.opshop.synces.mapper.product.IProductPropertyMapper;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryParamData;
import com.orderplus.opshop.synces.service.elasticsearch.data.QueryResultData;
import com.orderplus.opshop.synces.service.elasticsearch.impl.OpSearchService;
import com.orderplus.opshop.synces.service.product.ProductPropertyListService;
import com.orderplus.opshop.synces.service.product.ProductPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * remark
 *
 * @author mhj
 * @date 2019-04-22 11:19:34
 */
@Service("productPropertyService")
public class ProductPropertyServiceImpl extends ServiceImpl<IProductPropertyMapper, ProductProperty> implements ProductPropertyService {
	@Autowired
	private OpSearchService opSearchService;
	@Autowired
	private ProductPropertyListService productPropertyListService;

	@Override
	public List<ProductDetailModel> mapFromProductPropertyToProductDetailModel(Map<String, Object> map, String action, List<ProductDetailModel> productDetailModelList) {
		try {
//			把表名的字段名中包含_替换
			Map<String, Object> newMap = MapUtil.assemble(map);

//			转成product表的数据
			ProductProperty productProperty = (ProductProperty) MapUtil.mapToObject(newMap, ProductProperty.class);

			ProductDetailModel.ProductProperty productProps = setData(productProperty, action);

//			query key 格式: propertyListSrc.propId
//			String queryKey = new StringBuffer("propertyListSrc.propId").toString();
			String queryKey = new StringBuffer("propertyListIds.propId").toString();

			List<Map<String, Object>> queryResult = queryProductProps(productProps, queryKey);

			if (ValueUtil.isNotEmpty(queryResult)) {
				for (Map<String, Object> getMap : queryResult) {
					ProductDetailModel productDetailModel = new ProductDetailModel();
					String productId = ValueUtil.toStr(getMap.get(EsConstant.ES_PRIMARY_KEY));
					productDetailModel.setProductId(productId);

					List<Map<String, Object>> propertyListIdList = (List<Map<String, Object>>) getMap.get("propertyListIds");

					List<ProductDetailModel.ProductProperty> propertyList = productPropertyListService.convertMapToPropsList(propertyListIdList, action, productProps);

					propertyList.add(productProps);

//					productDetailModel.setPropertyListSrc(propertyList);
					productDetailModel.setPropertyListIds(propertyList);
					productDetailModelList.add(productDetailModel);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return productDetailModelList;
	}

	private List<Map<String, Object>> queryProductProps(ProductDetailModel.ProductProperty productProperty, String queryKey) throws IOException {
		List<Map<String, Object>> result = new ArrayList<>();

//		int count = opSearchService.queryTotal(EsConstant.ES_INDICES, new String[]{queryKey}, new String[]{productProperty.getPropId()});

//		int pages = 0;
//		int size = 1000;
//		if (count % size == 0) {
//			pages = count / size;
//		} else {
//			pages = count / size + 1;
//		}

//		for (int i = 0; i <= pages; i++) {
//			RequestParam<QueryParamData> request = opSearchService.setDefaultParam(EsConstant.ES_INDICES, new String[]{queryKey}, new Object[]{productProperty.getPropId()}, i, size);
		RequestParam<QueryParamData> request = opSearchService.setDefaultParam(EsConstant.ES_INDICES, new String[]{queryKey}, new Object[]{productProperty.getPropId()}, 0, 5000);

		ResponseData<QueryResultData.ListQueryResultData> response = opSearchService.call(request);

		result.addAll(response.getData().getResultDetailList());
//		}

		return result;
	}

	private ProductDetailModel.ProductProperty setData(ProductProperty productProperty, String action) {
		ProductDetailModel.ProductProperty productProps = new ProductDetailModel.ProductProperty();

		if (ValueUtil.isNotEmpty(productProperty.getPropertyId())) {
			productProps.setPropId(ValueUtil.toStr(productProperty.getPropertyId()));
		}

//		if ("update".equals(action)) {
		if (ValueUtil.isNotEmpty(productProperty.getPropertyKey())) {
			productProps.setPropKey(productProperty.getPropertyKey());
		}
		if (ValueUtil.isNotEmpty(productProperty.getPropertyKeyId())) {
			productProps.setPropKeyId(productProperty.getPropertyKeyId());
		}
		if (ValueUtil.isNotEmpty(productProperty.getPropertyKeyTh())) {
			productProps.setPropKeyTh(productProperty.getPropertyKeyTh());
		}
		if (ValueUtil.isNotEmpty(productProperty.getPropertyKeyZh())) {
			productProps.setPropKeyZh(productProperty.getPropertyKeyZh());
		}
//		} else {
//			productProps.setPropKey(null);
//			productProps.setPropKeyId(null);
//			productProps.setPropKeyTh(null);
//			productProps.setPropKeyZh(null);
//		}

		return productProps;
	}

	@Override
	public List<ProductDetailModel.ProductProperty> appendPropListToMapObject(String propListSrcIds, String action) {
		if (ValueUtil.isEmpty(propListSrcIds)) {
			return null;
		}

		List<ProductDetailModel.ProductProperty> list = new LinkedList<>();

		String[] propListIdArray = propListSrcIds.split(",");

		for (String propListSrcId : propListIdArray) {
			ProductDetailModel.ProductProperty.PropsValue propsValue = productPropertyListService.convertPropListIdsToMapObject(propListSrcId, action);

			if (null != propsValue) {
				ProductProperty productProperty = baseMapper.queryById(propsValue.getPropId());

				if (null != productProperty) {
					ProductDetailModel.ProductProperty productProps = new ProductDetailModel.ProductProperty();

					List<ProductDetailModel.ProductProperty.PropsValue> propsValueList = new LinkedList<>();

					productProps = setData(productProperty, action);

					propsValueList.add(propsValue);

					productProps.setPropValueList(propsValueList);

					list.add(productProps);
				}
			}
		}

		return list;
	}

	@Override
	public IPage<Map<String, Object>> pageMap(Page<Map<String, Object>> page) {
		return baseMapper.pageMap(page);
	}

}